package com.xiangyueEducation.uploaderCloud.controller;

import com.xiangyueEducation.uploaderCloud.pojo.dto.AnsDTO;
import com.xiangyueEducation.uploaderCloud.pojo.entity.Modules;
import com.xiangyueEducation.uploaderCloud.pojo.entity.Questions;
import com.xiangyueEducation.uploaderCloud.service.ModuleService;
import com.xiangyueEducation.uploaderCloud.service.QuestionService;
import com.xiangyueEducation.uploaderCloud.utils.Result;
import com.xiangyueEducation.uploaderCloud.utils.enums.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private ModuleService moduleService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private com.xiangyueEducation.uploaderCloud.utils.JwtHelper jwtHelper;

    //查询出来看看有没有试题，如果没有试题掉新增，有试题就掉更新接口
    @PostMapping("/teacher")
    public com.xiangyueEducation.uploaderCloud.utils.Result addQuestion(@RequestBody List<Questions> questions){
        Result result = questionService.checkQuestions(questions);
        if (result!=null)return result;
        //查询成功，去插入，之后返回
        //将唯一的一个id去查数据库，看是否存在此模块
        boolean flag = moduleService.queryExistById(questions.get(0).getTestId())!=null;
        if (flag){
            questionService.insertBatch(questions);
            return com.xiangyueEducation.uploaderCloud.utils.Result.ok(ResultCodeEnum.SUCCESS);
        }
        //查询失败，直接返回
        return com.xiangyueEducation.uploaderCloud.utils.Result.build(null, ResultCodeEnum.TEST_ID_ERROR);
    }
    // question不分页
    @GetMapping("/all/{moduleId}")
    public Result getQuestions(@PathVariable("moduleId")Long moduleId){
        //拿着模块id直接去查，如果查不到，说明只创建了该模块，但并没有为此模块创建试题，因此返回一个什么都没有的空集合，让前端跳转到响应的模块（老师创建，学生提示未创建）
        //无需进行任何校验，因此直接进入
        if (moduleId==null) {
            return Result.build(null,ResultCodeEnum.TEST_ID_ERROR);
        }
        List<Questions> questionsVos = questionService.queryByModuleId(moduleId);
        return Result.ok(questionsVos);
    }
    //批量修改试题
    //因为更新牵扯有的题是update，有的题是delete，有的题又是insert，
    //因此选择将新增和更新两个接口分开，查出来有题存在就是更新，要在数据库中删了，再插入新选进来的，发过来来的如果是空数据，就说明全部都删了
    //删除时，又要维护题号连续，因此，批量删除也视为一次批量修改
    //完成批量修改试题
    @PutMapping("/teacher")
    public Result updateQuestion( @RequestBody List<Questions> questions){
        Result errorResult = questionService.checkQuestions(questions);
        if (errorResult!=null)return errorResult;
        questionService.updateQuestion(questions);
        return com.xiangyueEducation.uploaderCloud.utils.Result.ok(ResultCodeEnum.SUCCESS);
        }

    @PostMapping("/student/doTest")
    public Result doTest(@RequestBody AnsDTO quizDTO,@RequestHeader("token") String token){
        Long userId = jwtHelper.getUserId(token);
        Modules modules = moduleService.queryExistById(quizDTO.getTestId());
        if (modules==null){
            return com.xiangyueEducation.uploaderCloud.utils.Result.build(null, ResultCodeEnum.TEST_ID_ERROR);
        }
        return questionService.doTest(quizDTO, userId, modules);
    }

}

